package com.system.controller;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JMap;
import com.jfinal.plugin.activerecord.SqlPara;
import com.system.model.SystemUser;

import utils.MD5;

/**
 * 系统用户控制类 
 * @author whzhang
 * @since 2017-04-18
 * @version 1.0
 *
 */
public class UserController extends Controller {
	private SystemUser userDao = SystemUser.dao;
	
	public void index(){
		//TODO forward to loginShow action.
		
		renderText("Test.");
	}
	
	public void login(){
		String username = getPara("username");
		String password = getPara("password");
		password = MD5.digest(password);
		
		JMap cond = JMap.create("username", username).set("password", password);
		SqlPara sp = SystemUser.dao.getSqlPara("user.login", cond);
		
//		List<SystemUser> users = userDao.find("select * from systemUser where account = ? and password = ?",
//				username, password);
		List<SystemUser> users = userDao.find(sp);
		if(users.isEmpty()){
			renderText("用户名或密码错误！");
		}else{
			renderText(String.format("欢迎您，%s！",users.get(0).get("userName")));
		}
	}
	
	public void loginShow(){
		//TODO forward to login page.
	}
}
